package com.ruoyi.dispatch.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dispatch.domain.MonthTunnelDayReport;
import org.springframework.beans.BeanUtils;
import com.ruoyi.dispatch.domain.TunnelDayReport;
import com.ruoyi.dispatch.enums.TunnelDayReportCol;
import com.ruoyi.dispatch.mapper.TunnelDayReportMapper;
import com.ruoyi.dispatch.service.ITunnelDayReportService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 掘进日报Service业务层处理
 * 
 * @author wuxiaofang
 * @date 2025-04-08
 */
@Service
public class TunnelDayReportServiceImpl implements ITunnelDayReportService 
{
    @Autowired
    private TunnelDayReportMapper tunnelDayReportMapper;

    /**
     * 查询掘进日报
     * 
     * @param reportId 掘进日报主键
     * @return 掘进日报
     */
    @Override
    public TunnelDayReport selectTunnelDayReportByReportId(Long reportId)
    {
        return tunnelDayReportMapper.selectTunnelDayReportByReportId(reportId);
    }

    /**
     * 查询掘进日报列表
     * 
     * @param tunnelDayReport 掘进日报
     * @return 掘进日报
     */
    @Override
    public List<TunnelDayReport> selectTunnelDayReportList(TunnelDayReport tunnelDayReport)
    {
        return tunnelDayReportMapper.selectTunnelDayReportList(tunnelDayReport);
    }

    /**
     * 新增掘进日报
     * 
     * @param tunnelDayReport 掘进日报
     * @return 结果
     */
    @Override
    public int insertTunnelDayReport(TunnelDayReport tunnelDayReport)
    {
        tunnelDayReport.setCreateTime(DateUtils.getNowDate());
        return tunnelDayReportMapper.insertTunnelDayReport(tunnelDayReport);
    }

    /**
     * 修改掘进日报
     * 
     * @param tunnelDayReport 掘进日报
     * @return 结果
     */
    @Override
    public int updateTunnelDayReport(TunnelDayReport tunnelDayReport)
    {
        tunnelDayReport.setUpdateTime(DateUtils.getNowDate());
        return tunnelDayReportMapper.updateTunnelDayReport(tunnelDayReport);
    }

    /**
     * 批量删除掘进日报
     * 
     * @param reportIds 需要删除的掘进日报主键
     * @return 结果
     */
    @Override
    public int deleteTunnelDayReportByReportIds(Long[] reportIds)
    {
        return tunnelDayReportMapper.deleteTunnelDayReportByReportIds(reportIds);
    }

    /**
     * 删除掘进日报信息
     * 
     * @param reportId 掘进日报主键
     * @return 结果
     */
    @Override
    public int deleteTunnelDayReportByReportId(Long reportId)
    {
        return tunnelDayReportMapper.deleteTunnelDayReportByReportId(reportId);
    }

    @Override
    public List<Map<String, String>> handleColumnMap(String[] columnArray) {
        List<Map<String, String>> columns = Lists.newArrayList();
        for (String item : columnArray) {
            Map column = TunnelDayReportCol.getByCode(item);
            if (column != null) {
                columns.add(column);
            }
        }
        return columns;
    }

    @Override
    public List<MonthTunnelDayReport> countMonthYearTunnelDayReport(TunnelDayReport tunnelDayReport) {
        SimpleDateFormat sourceFormatMonth = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat sourceFormatYear = new SimpleDateFormat("yyyy");
        List<TunnelDayReport> tunnelDayReportList = tunnelDayReportMapper.selectTunnelDayReportList(tunnelDayReport);
        String month = sourceFormatMonth.format(tunnelDayReport.getCurrentDate());
        String year = sourceFormatYear.format(tunnelDayReport.getCurrentDate());
        List<MonthTunnelDayReport> monthList = tunnelDayReportMapper.countMonthTunnel(month);
        List<MonthTunnelDayReport> yearList = tunnelDayReportMapper.countYearTunnel(year);
        for (TunnelDayReport dayReport : tunnelDayReportList) {
            for (MonthTunnelDayReport monthTunnelDayReport : monthList) {
                if (null != dayReport && null != monthTunnelDayReport) {
                    if (dayReport.getTeamId().equals(monthTunnelDayReport.getTeamId()) && dayReport.getTunnelId().equals(monthTunnelDayReport.getTunnelId())){

                        BeanUtils.copyProperties( dayReport,monthTunnelDayReport);
                        monthTunnelDayReport.setDifference(dayReport.getDrivingLength().subtract(dayReport.getDrivingPlanLength()));
                    }

                }
                for (MonthTunnelDayReport yearTunnelDayReport : yearList) {
                    if (yearTunnelDayReport.getTeamId().equals(monthTunnelDayReport.getTeamId()) && yearTunnelDayReport.getTunnelId().equals(dayReport.getTunnelId())){
                        monthTunnelDayReport.setYearDrivingLength(yearTunnelDayReport.getYearDrivingLength());
                        monthTunnelDayReport.setYearDrivingPlanLength(yearTunnelDayReport.getYearDrivingPlanLength());

                    }
                }
            }

        }
        return monthList;
    }
}
